{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using SymPy\n",
    "using LinearAlgebra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  Levi-Civita记号\n",
    "ε= [ Sym((length(Set([i,j,k]))==3)*       #  存在重复指标，取0\n",
    "    (-1)^(sum([i,j,k][σ]>[i,j,k][ρ]   \n",
    "            for σ in 1:3,ρ in  1:3  if σ<ρ) % 2)) # 计算逆序数。偶数取1，奇数取-1\n",
    "    for  i in 1:3, j in 1:3, k in 1:3 ] ;\n",
    "\n",
    "# 单位矩阵\n",
    "δ = sympy.diag(1,1,1);\n",
    "\n",
    "# so(3)结构常数就是Levi-Civita记号【以前计算过】\n",
    "C = ε;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "待选$A^\\mu$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}a_{1}\\\\a_{2}\\\\a_{3}\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "3-element Array{Sym,1}:\n",
       " a_1\n",
       " a_2\n",
       " a_3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@vars a_1 a_2 a_3\n",
    "\n",
    "A = Sym[a_1,a_2,a_3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "待求特征方程\n",
    "\n",
    "$$\n",
    "A^\\mu  C^\\rho_{\\ \\ \\mu\\upsilon}X^\\upsilon = \\lambda X^\\rho\n",
    "$$\n",
    "\n",
    "对应的算符矩阵\n",
    "\n",
    "$$(\\mathrm{ad}_A)^\\rho_{\\ \\ \\upsilon} =A^\\mu  C^\\rho_{\\ \\ \\mu\\upsilon}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrr}0&- a_{3}&a_{2}\\\\a_{3}&0&- a_{1}\\\\- a_{2}&a_{1}&0\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "3×3 Array{Sym,2}:\n",
       "    0  -a_3   a_2\n",
       "  a_3     0  -a_1\n",
       " -a_2   a_1     0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ad_A = [sum(A[μ]*C[ρ,μ,υ] for μ in 1:3)\n",
    "            for ρ in 1:3, υ in 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}\\sqrt{- a_{1}^{2} - a_{2}^{2} - a_{3}^{2}}\\\\- \\sqrt{- a_{1}^{2} - a_{2}^{2} - a_{3}^{2}}\\\\0\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "3-element Array{Sym,1}:\n",
       "  sqrt(-a_1^2 - a_2^2 - a_3^2)\n",
       " -sqrt(-a_1^2 - a_2^2 - a_3^2)\n",
       "                             0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本征根【用于观察】\n",
    "eigvals(ad_A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "观察结果：根号内取正数时，具有最大非重根数（实数根），比如可以取\n",
    "\n",
    "$$\n",
    "A^\\mu=(0,0,i a),\\quad a>0\n",
    "$$\n",
    "\n",
    "对应的算符矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrr}0&- i a&0\\\\i a&0&0\\\\0&0&0\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "3×3 Array{Sym,2}:\n",
       "   0  -I*a  0\n",
       " I*a     0  0\n",
       "   0     0  0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@vars  a real=true positive=true\n",
    "ad_A = ad_A  .|> (expr->expr(a_1=>0,a_2=>0,a_3=>IM*a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Tuple{Sym,Int64,Array{Array{Sym,2},1}},1}:\n",
       " (0, 1, [[0; 0; 1]])\n",
       " (-a, 1, [[I; 1; 0]])\n",
       " (a, 1, [[-I; 1; 0]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最后确定本征根和本征矢\n",
    "# eigvecs(ad_A,vals)  这个被注释方法在julia中有bug\n",
    "vects = ad_A.eigenvects()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}0\\\\- a\\\\a\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "3-element Array{Sym,1}:\n",
       "  0\n",
       " -a\n",
       "  a"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[vects[i][1] for i in 1:3 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrr}0&0&1\\\\i&1&0\\\\- i&1&0\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "3×3 Array{Sym,2}:\n",
       "  0  0  1\n",
       "  I  1  0\n",
       " -I  1  0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 每行对应一个特征矢\n",
    "[vects[i][3][1][j] for i in 1:3 , j in 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.4.1",
   "language": "julia",
   "name": "julia-1.4"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
